# coding=utf-8
from time import sleep

import RPi.GPIO as GPIO

from config import Config
from photoelectricsensor import PhotoElectricSensor

'''
步进电机控制类
'''


class Stepper:
    def __init__(self, pulGpio, dirGpio, hz, dir):
        self.pulGpio = int(pulGpio)
        self.dirGpio = int(dirGpio)
        self.hz = int(hz)
        self.dir = int(dir)
        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(self.pulGpio, GPIO.OUT)
        GPIO.setup(self.dirGpio, GPIO.OUT)

    def initMCX(self):
        '''
        初始化 MCX 电机
        :return:
        '''
        p = GPIO.PWM(self.pulGpio, self.hz)
        GPIO.output(self.dirGpio, True)
        # 回归原点
        while True:
            p.start(0.001)
            sleep(0.0001)
            photoElectricSensor = PhotoElectricSensor(Config().read("PhotoElectric", "SCX_pin"))
            if photoElectricSensor.checkObstacle() == 1:
                p.stop()
                break

    def initMCY(self):
        '''
        初始化MCY 电机
        :return:
        '''
        p = GPIO.PWM(self.pulGpio, self.hz)
        GPIO.output(self.dirGpio, True)
        # 回归原点
        while True:
            p.start(0.001)
            sleep(0.0001)
            photoElectricSensor = PhotoElectricSensor(Config().read("PhotoElectric", "SCY_pin"))
            if photoElectricSensor.checkObstacle() == 1:
                p.stop()
                break

    def initMA(self):
        '''
        初始化MA 电机
        :return:
        '''
        self.ma(0)

    def spinMotorAC(self, step, hz):
        '''
        反转
        :param step: 步数
        :param DC: 频率
        :return:
        '''
        p = GPIO.PWM(self.pulGpio, hz)
        GPIO.output(self.dirGpio, False)
        while step > 0:
            p.start(0.001)
            sleep(0.0001)
            step -= 1
        p.stop()

    def spinMotorC(self, step, hz):
        '''
        正转
        :param step:步数
        :param DC:频率
        :return:
        '''
        hz = int(hz)
        p = GPIO.PWM(self.pulGpio, hz)
        GPIO.output(self.dirGpio, True)
        while step > 0:
            p.start(0.001)
            sleep(0.0001)
            step -= 1
        p.stop()

    def turn(self, dir, step):
        '''
        转动方向控制
        M-BL/M-BM/M-BR
        可以采用此函数
        :param dir:方向 0 正方向，1反方向
        :param step:步数
        :param hz:频率 1000以上
        :return:
        '''
        if dir == 0:
            self.spinMotorAC(step, self.hz)
        elif dir == 1:
            self.spinMotorC(step, self.hz)
        else:
            self.spinMotorC(step, self.hz)

    global zero
    global one
    global two

    def ma(self, point):
        '''
        根据 输入 0，1，2 不同节点自动向节点靠拢
        :param point: 0，1，2节点参数
        :return:
        '''
        zero = False
        one = False
        two = False

        if point == None: point = 0

        if point == 1:
            if zero == True or one == False and two == False:
                # 从原点出发去1 点
                p = GPIO.PWM(self.pulGpio, self.hz)
                GPIO.output(self.dirGpio, True)
                # 从0点到1点
                while True:
                    p.start(0.001)
                    sleep(0.0001)
                    photoElectricSensor = PhotoElectricSensor(Config().read("PhotoElectric", "SAM_pin"))
                    if photoElectricSensor.checkObstacle() == 1:
                        p.stop()
                        zero = False
                        one = True
                        two = False
                        break
            elif zero == False and one == False and two == True:
                # 从2点去往1点
                p = GPIO.PWM(self.pulGpio, self.hz)
                GPIO.output(self.dirGpio, False)
                # 从2点到1点
                while True:
                    p.start(0.001)
                    sleep(0.0001)
                    photoElectricSensor = PhotoElectricSensor(Config().read("PhotoElectric", "SAM_pin"))
                    if photoElectricSensor.checkObstacle() == 1:
                        p.stop()
                        zero = False
                        one = True
                        two = False
                        break

        elif point == 2:
            if zero == True or one == True and two == False:
                # 从0点出发去2点
                p = GPIO.PWM(self.pulGpio, self.hz)
                GPIO.output(self.dirGpio, True)
                # 从0点到2点
                while True:
                    p.start(0.001)
                    sleep(0.0001)
                    photoElectricSensor = PhotoElectricSensor(Config().read("PhotoElectric", "SAH_pin"))
                    if photoElectricSensor.checkObstacle() == 1:
                        p.stop()
                        zero = False
                        one = False
                        two = True
                        break

        elif point == 0:
            if zero == False and one == True or two == True:
                p = GPIO.PWM(self.pulGpio, self.hz)
                GPIO.output(self.dirGpio, False)
                # 从1点回归0点
                while True:
                    p.start(0.001)
                    sleep(0.0001)
                    photoElectricSensor = PhotoElectricSensor(Config().read("PhotoElectric", "SAL_pin"))
                    if photoElectricSensor.checkObstacle() == 1:
                        p.stop()
                        zero = True
                        one = False
                        two = False
                        break
        else:
            if zero == False and one == True or two == True:
                p = GPIO.PWM(self.pulGpio, self.hz)
                GPIO.output(self.dirGpio, False)
                # 从1点回归0点
                while True:
                    p.start(0.001)
                    sleep(0.0001)
                    photoElectricSensor = PhotoElectricSensor(Config().read("PhotoElectric", "SAL_pin"))
                    if photoElectricSensor.checkObstacle() == 1:
                        p.stop()
                        zero = True
                        one = False
                        two = False
                        break


if __name__ == "__main__":
    # 使用案例
    # 1.创建点击对象，2.设置转向并设置步数与频率
    pulpin = int(Config().read("Stepper", "MA_pul_pin"))
    dirpin = int(Config().read("Stepper", "MA_dir_pin"))
    hz = int(Config().read("Stepper", "MA_hz"))
    MA_REVERCE = int(Config().read("Stepper", "MA_REVERCE"))
    print(hz)
    stepper = Stepper(pulpin, dirpin, hz, MA_REVERCE)
    # stepper.turn(MA_REVERCE, 40)

    stepper.turn(0, 7025)  # 一圈
    # stepper.turn(1, 40)

    GPIO.cleanup()
